package service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.support.annotation.Nullable;

import ConnectState.NetWorkState;
import Uitls.ToastUtil;
import bean.Type;

/**
 * Created by Administrator on 2016/7/15.
 */
public class NetworkStateService extends Service {

    NetWorkState mNetWorkState = new NetWorkState();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //注册一个广播接收者，监听网络状况的变化
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(connectionReceiver, intentFilter);
    }

    /**
     * 定义一个Receiver重载其中的onReceive函数，在其中完成所需要的功能，
     * 如根据WIFI和GPRS是否断开来改变空间的外观
     * */
    private BroadcastReceiver connectionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //用于判断网络连接的类型
            ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

           /* if(!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
                //unconnect network
                ToastUtil.showToast(NetworkStateService.this,"当前没有连接");
            } else {
                //connect network
                if(mNetWorkState.isWifiConnected(NetworkStateService.this)) {
                    // currentconnect state wifi
                    ToastUtil.showToast(NetworkStateService.this,"当前连接状态是wifi连接");
                } else if(mNetWorkState.isMobileConnected(NetworkStateService.this)) {
                    //currentconnect state mobile
                    ToastUtil.showToast(NetworkStateService.this,"当前状态是手机流量连接");
                }
            }*/

            switch (Type.getAPNType(context)) {
                case -1://没有网络连接
                    ToastUtil.showToast(NetworkStateService.this,"当前连接状态无网络连接");
                    break;
                case 1://wifi网络
                    ToastUtil.showToast(NetworkStateService.this,"当前连接状态是wifi连接");
                    break;
                case 2://wap网络
                    ToastUtil.showToast(NetworkStateService.this,"当前连接状态是wap连接");
                    break;
                case 3://net网络
                    ToastUtil.showToast(NetworkStateService.this,"当前连接状态是net连接");
                    break;
            }
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //取消注册
        if (connectionReceiver != null) {
            unregisterReceiver(connectionReceiver);
        }
    }
}
